﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.View;
using CatUtilsDll.Utils.Currency.WebUtils;
using Newtonsoft.Json;
using RabbitDownload.Helper;
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows.Forms;

namespace RabbitDownload.WebSite.Pixiv.PixivDay
{
    /// <summary>
    /// 日榜扫描
    /// </summary>
    internal class PixivDayScanner
    {/// <summary>
     /// 启动日榜扫描
     /// </summary>
     /// <param name="Day"></param>
     /// <param name="API"></param>
     /// <param name="ImgScore"></param>
     /// <param name="MoedeTip">将会在文件名显示出来</param>
     /// <param name="AutoRun"></param>
     /// <returns></returns>
        public static string Start(String Day, String API, int ImgScore, string MoedeTip, bool AutoRun = false)
        {
            Console.WriteLine(Day);
            API = string.Format(API, Day, "{0}");

            List<string> ALLImg = new List<string>();

            for (int i = 1; i <= 2; i++)
            {
                Console.WriteLine(string.Format(API, i));

                List<string> Listtemp = AnalysisJson(string.Format(API, i), ImgScore, AutoRun);

                if (Listtemp == null)
                {
                    //这种情况为用户选择跳出
                    break;
                }
                else
                {
                    ALLImg.AddRange(Listtemp);
                }
            }
            if (ALLImg.Count != 0)

                return WebSite.WebCurrency.SaveHelper.AutoSave(ALLImg, Day + MoedeTip, "P站日榜");
            else
                return "";
        }

        /// <summary>
        /// 获取正确的API
        /// </summary>
        /// <returns></returns>
        public static string GetAPI(String ModeText)
        {
            switch (ModeText)
            {
                case "每日":
                    return PixivStaticValue.DayAPI;

                case "每日男性":
                    return PixivStaticValue.DayMaleAPI;

                case "每日女性":
                    return PixivStaticValue.DayFemaleAPI;

                case "周榜":
                    return PixivStaticValue.DayWeeklyAPI;

                case "月榜":
                    return PixivStaticValue.DayMonthlyAPI;

                case "R18":
                    return PixivStaticValue.DayR18;

                default:
                    return PixivStaticValue.DayAPI;
            }
        }

        /// <summary>
        /// 解析数据，在日期错误的情况下，会返回NULL而非一个正确的数组
        /// </summary>
        /// <param name="API"></param>
        /// <param name="ImgScore"></param>
        /// <param name="AutoRunFalg">如果为True，则会跳过提示，自动运行扫描</param>
        /// <returns></returns>
        private static List<string> AnalysisJson(string API, int ImgScore, bool AutoRunFalg)
        {
            List<string> Temp = new List<string>();
            Console.WriteLine(API);
            WebHeaderCollection webHeader = WebHelper.PcWebHeader();

            string Shtml = WebHelper.HttpGet(API, webHeader);

            if (TextHelper.JudgeNull(Shtml))
            {
                if (AutoRunFalg)
                    return null;
                ;
                if (MessageHelper.DialogYesNoCancle("数据为空，可能是选择了错误的天数，\r\n日本和中国有一天时差，请主动减去。\r\n请修改时间后再次开始，\r\n点击确定马上结束，\r\n取消则继续。",
                    "继续扫描", "停止扫描", "停止扫描", 5, DialogResult.OK, "P站获取到数据为空") == DialogResult.OK)
                    return null;
            }

            Shtml = new PixivHelper().StandartJson(Shtml);
            try
            {
                PixivDayJson.Root AllJson = JsonConvert.DeserializeObject<PixivDayJson.Root>(Shtml);

                if (AllJson.contents == null)
                {
                    PrintLog.E("获取内容为空！");
                    Console.WriteLine(Shtml);
                }
                else
                    foreach (PixivDayJson.ContentsItem SingleItem in AllJson.contents)
                    {
                        if (PixivStaticValue.ExcludeOpenFlag && JudgeTagAndUser(SingleItem))
                            continue;

                        Console.WriteLine("图总数" + SingleItem.illust_page_count);
                        if (SingleItem.rating_count < ImgScore)
                        {
                            Console.WriteLine("评分低于预设值，跳过");
                            continue;
                        }
                        Temp.AddRange(new PixivHelper().QuickAnalysisAllImg(SingleItem.illust_page_count, SingleItem.url, SingleItem.illust_id, ImgScore.ToString()));
                        Console.WriteLine("已缓存数量：" + Temp.Count);
                    }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }
            return Temp;
        }

        /// <summary>
        /// 屏蔽模式辅助
        /// </summary>
        /// <param name="singleItem"></param>
        /// <returns></returns>
        private static bool JudgeTagAndUser(PixivDayJson.ContentsItem singleItem)
        {
            return ListHelper.CompareRepetition(singleItem.tags, StaticValueOld.ExcludeList);
        }
    }
}